package com.mldkhn.led_controller;

import java.io.BufferedWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;

import com.larswerkman.colorpicker.ColorPicker;
import com.larswerkman.colorpicker.SaturationBar;
import com.larswerkman.colorpicker.ValueBar;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;

public class Fragment_Einzelfarbe extends Fragment implements OnClickListener {
	
	private static Button btnOneStart, btnOneStop;
	private static ColorPicker picker;
	private static SaturationBar saturationBar;
	private static ValueBar valueBar;
	private static String strOne = "ONE";
	private static String strStop = "OFF";
	private String colorHEX;
	private int color = 0;
	
	private static int SERVERPORT;
	private static String SERVER_IP;
	private Socket socket;
	Thread socketThread;
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
	    Bundle savedInstanceState) {
	    View v = inflater.inflate(R.layout.fragment_einzelfarbe, null);
	    picker = (ColorPicker) v.findViewById(R.id.colorPickerOne);
	    saturationBar = (SaturationBar) v.findViewById(R.id.saturationBarOne);
	    valueBar = (ValueBar) v.findViewById(R.id.valueBarOne);
	    btnOneStart = (Button) v.findViewById(R.id.btnOneStart);
	    btnOneStop = (Button) v.findViewById(R.id.btnOneStop);
	    btnOneStart.setOnClickListener(this);
	    btnOneStop.setOnClickListener(this);
	    picker.addSaturationBar(saturationBar);
	    picker.addValueBar(valueBar);
		try{
			SharedPreferences mySharedPreferences = PreferenceManager.getDefaultSharedPreferences(v.getContext());
			SERVER_IP = mySharedPreferences.getString("edittext_ip", "");
			SERVERPORT = Integer.parseInt(mySharedPreferences.getString("edittext_port", ""));
			socketThread = new Thread(new ClientThread());
			socketThread.start();
			PrintWriter out = new PrintWriter(new BufferedWriter(
					new OutputStreamWriter(socket.getOutputStream())),
					true);
			out.println("HELLO");
			out.close();
			socketThread.interrupt();
			socketThread = null;
			
			} 
			  catch (UnknownHostException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			} catch (Exception e) {
				e.printStackTrace();
			}
	    return v;
	}

	@Override
	public void onClick(View v) {
		switch(v.getId()){
		case R.id.btnOneStart:
			color = picker.getColor();
			colorHEX = Integer.toHexString(color);
			picker.setOldCenterColor(picker.getColor());
			try{
				SharedPreferences mySharedPreferences = PreferenceManager.getDefaultSharedPreferences(v.getContext());
				SERVER_IP = mySharedPreferences.getString("edittext_ip", "");
				SERVERPORT = Integer.parseInt(mySharedPreferences.getString("edittext_port", ""));
				socketThread = new Thread(new ClientThread());
				socketThread.start();
				PrintWriter out = new PrintWriter(new BufferedWriter(
						new OutputStreamWriter(socket.getOutputStream())),
						true);
				out.println(strOne + "\n" + colorHEX);
				out.close();
				socketThread.interrupt();
				socketThread = null;
				
				} 
				  catch (UnknownHostException e) {
					e.printStackTrace();
				} catch (IOException e) {
					e.printStackTrace();
				} catch (Exception e) {
					e.printStackTrace();
				}

            break;
        case R.id.btnOneStop:
			try{
				SharedPreferences mySharedPreferences = PreferenceManager.getDefaultSharedPreferences(v.getContext());
				SERVER_IP = mySharedPreferences.getString("edittext_ip", "");
				SERVERPORT = Integer.parseInt(mySharedPreferences.getString("edittext_port", ""));
				socketThread = new Thread(new ClientThread());
				socketThread.start();
				PrintWriter out = new PrintWriter(new BufferedWriter(
						new OutputStreamWriter(socket.getOutputStream())),
						true);
				out.println(strStop);
				socketThread.interrupt();
				socketThread = null;
				
				} 
				  catch (UnknownHostException e) {
					e.printStackTrace();
				} catch (IOException e) {
					e.printStackTrace();
				} catch (Exception e) {
					e.printStackTrace();
				}
        	
		}
		
	}
	
class ClientThread implements Runnable {
		
		@Override
		public void run() {
			
			try {
				InetAddress serverAddr = InetAddress.getByName(SERVER_IP);

				socket = new Socket(serverAddr, SERVERPORT);

			} catch (UnknownHostException e1) {
				e1.printStackTrace();
			} catch (IOException e1) {
				e1.printStackTrace();
			}
		}
	}
}
